package com.lcf.app.validation.controller;

import com.lcf.app.validation.model.dto.Save;
import com.lcf.app.validation.model.dto.UserDTO;
import com.lcf.common.web.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @author ChenFei
 * @date 2021/10/25
 * <p>
 * 数据效验测试
 */
@Validated
@RestController
@RequestMapping("valid")
public class ValidationController {

    /**
     * 功能描述：简单参数效验：需要在类上添加@Validated注解
     */
    @RequestMapping("param")
    public Result<String> test1(@NotBlank(message = "用户名不能为空") String username) {

        return Result.success().setData(username);
    }

    /**
     * 功能描述：自定义类型验证,自定义参数前添加@Valid，不会验证带有groups属性
     */
    @RequestMapping("body")
    public String test2(@Valid @RequestBody UserDTO dto) {

        return "ok";
    }

    /**
     * 功能描述：自定义类型验证,分组需要使用@Validated,指定效验,只会验证Save.class
     */
    @RequestMapping("group")
    public String test3(@Validated({Save.class}) @RequestBody UserDTO dto) {

        return "ok";
    }


}
